#include "cpuranklistmainwindow.h"
#include "ui_cpuranklistmainwindow.h"

#include <httpclient.h>

#include <src/ui/widgets/cpurankviewmodel.h>

CpuRankListMainWindow::CpuRankListMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CpuRankListMainWindow)
{
    ui->setupUi(this);

    Response resp = HttpClient::instance().get("https://cpu.bmcx.com/web_system/bmcx_com_www/system/file/cpu/get_data/?lx=cpu&s=1&e=999999");
    if (resp.ok()) {

        for (const auto &var : resp.toList()) {
            auto item = new QListWidgetItem(CpuRankInfo(var.toMap()).ming_cheng);
            ui->cpu_rank_list->addItem(item);
        }

        // 请求完成后回到主线程更新UI
        // QMetaObject::invokeMethod(cpu_rank_model, "layoutChanged", Qt::QueuedConnection);
    }

    connect(ui->cpu_filter, &QLineEdit::textChanged, this, [=](const QString &text){
        int count = ui->cpu_rank_list->count();
        for (int i = 0; i < count; ++i) {
            auto item = ui->cpu_rank_list->item(i);

            if (text.isEmpty()) {
                item->setHidden(false);
                continue;
            }

            item->setHidden(!item->text().contains(text,Qt::CaseInsensitive));
        }
    });
}

CpuRankListMainWindow::~CpuRankListMainWindow()
{
    delete ui;
}
